Docker
CLI
tool
container
Docker is a platform for developing, shipping, and running applications in isolated environments.
The key components are:
- Container: A lightweight, standalone, executable package that includes everything needed to run a piece of software
- Image: A read-only template with instructions for creating a container, built from layers
- Dockerfile: A text file containing commands to build a Docker image automatically
- Registry: A repository for Docker images (like Docker Hub)
- Volume: A persistent storage space shared between host and container
- Network: Enables containers to communicate with each other and external resources
Dockerfile
Dockerfile example:
FROM rocker/shiny:latest
RUN apt-get update
RUN apt-get install -qq -y libpq-dev
RUN install2.r ggplot2 shiny shinydashboard \
shinydashboardPlus \
dplyr RPostgresExplanation:
FROMspecifies the base imageRUNexecutes a command in the container
Basic Docker Commands
| Description | Command |
|---|---|
| Start application | docker run <container-name/container-id> |
| List containers | docker ps |
| List docker images | docker images |
| Pull images from registry | docker pull |
| Kill container | docker kill <container-name/container-id> |
| Build image based on Dockerfile | docker build <dir-with-docker-file> |
| Execute command inside container | docker exec <command> <container> |
Interacting with Containers
Start a container and execute bash:
docker run -d rocker/shiny
docker exec -it rocker/shiny /bin/bashStart R directly in a container:
docker exec -it rocker/shiny /usr/local/bin/RContainer Management
| Description | Command |
|---|---|
| Stop container | docker stop <container-name/container-id> |
| Remove container | docker rm <container-name/container-id> |
| Remove container and its volumes | docker rm -v <container-name/container-id> |
| Restart container | docker restart <container-name/container-id> |
Docker Compose
Docker Compose is a tool for defining and running multi-container Docker applications using a YAML file. Example docker-compose.yml:
services:
postgres:
container_name: db_container
build: ./postgres
restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
ports:
- "5432:5432"
volumes:
- "./pgdata:/var/lib/postgresql/data"
shiny:
container_name: shiny
depends_on:
- postgres
build: ./shiny
volumes:
- "./shiny-logs:/var/log/shiny-server"
- "./shiny-home:/srv/shiny-server"
ports:
- "3838:3838"Run docker-compose:
docker compose up --force-recreateDebugging Tips
View container logs:
docker logs <container-name>Prevent container from crashing for debugging: Add to docker-compose.yml:
command: "sleep infinity"Container Status
| Description | Command |
|---|---|
| List running containers (with grep filter) | docker ps \| grep <container-name> |
| Show all containers including stopped ones | docker ps -a |
Container Logs:
| Description | Command |
|---|---|
| View container logs | docker logs <container-name> |
| Follow container logs live | docker logs -f <container-name> |
| Show last N lines of logs | docker logs --tail=100 <container-name> |
Network Debugging:
| Description | Command |
|---|---|
| Check port mappings | netstat -tulpn | grep <port-number> |
| Test container connectivity | curl -v http://localhost:<port> |
| List all Docker networks | docker network ls |
| Inspect network settings | docker network inspect <network-name> |
Container Information:
| Description | Command |
|---|---|
| Show detailed container info | docker inspect <container-name> |
| Show container resource usage | docker stats <container-name> |
| Show processes running in container | docker top <container-name> |
Troubleshooting Commands:
| Description | Command |
|---|---|
| Check if container can reach internet | docker exec <container-name> ping 8.8.8.8 |
| Get shell access inside container | docker exec -it <container-name> /bin/bash |
| Check container logs since specific time | docker logs --since 30m <container-name> |
| Check container environment variables | docker exec <container-name> env |
Volume Inspection:
| Description | Command |
|---|---|
| List volumes | docker volume ls |
| Inspect volume | docker volume inspect <volume-name> |
| Check volume mount points | docker inspect -f '{{ .Mounts }}' <container-name> |